<?php

declare(strict_types=1);

namespace App\Service\Business;

use App\Service\Access\OperateLogAccess;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use function Hyperf\Coroutine\co;

/**
 * 操作日志-业务类
 */
class OperateLogBusiness extends BaseBusiness
{
        #[Inject]
        protected OperateLogAccess $OperateLogAccess;

        /**
         * 创建
         * @param array $operateBefore
         * @param array $operateAfter
         * @throws ContainerExceptionInterface
         * @throws NotFoundExceptionInterface
         */
        public function create(array $operateBefore = [], array $operateAfter = []): void
        {
                $arrInsert = [
                        'id' => snowflakeId(),
                        'ip' => $this->request->getHeader('x-real-ip')[0] ?? '',
                        'path' => $this->request->getUri()->getPath(),
                        'method' => $this->request->getMethod(),
                        'params' => json_encode($this->request->all(), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
                        'operateId' => $this->request->getAttribute('userId'),
                        'operateBefore' => json_encode($operateBefore, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
                        'operateAfter' => json_encode($operateAfter, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
                        'createTime' => $this->request->getAttribute('timestamp')
                ];
                co(fn() => $this->OperateLogAccess->insert($arrInsert));
        }

        /**
         * 分页
         * @param array $inject
         * @return array
         */
        public function paginate(array $inject = []): array
        {
                $arrQueryResult = $this->OperateLogAccess->paginate($inject);
                $arrResult = [
                        'page' => $arrQueryResult['current_page'],
                        'limit' => $arrQueryResult['per_page'],
                        'total' => $arrQueryResult['total'],
                        'list' => $arrQueryResult['data'],
                ];
                return businessSuccess($arrResult);
        }

        /**
         * 删除
         * @param array $inject
         * @return array
         */
        public function delete(array $inject = []): array
        {
                $intEffectRow = $this->OperateLogAccess->delete($inject ?: [
                        'wheres' => ['id' => $this->request->input('id')]
                ]);
                if (!$intEffectRow) return businessError();
                return businessSuccess();
        }

        /**
         * 批量删除
         * @param array $inject
         * @return array
         */
        public function batchDelete(array $inject = []): array
        {
                $intEffectRow = $this->OperateLogAccess->delete($inject ?: [
                        'wheres' => ['ids' => $this->request->input('ids', [])]
                ]);
                if (!$intEffectRow) return businessError();
                return businessSuccess();
        }
}